package com.sdy.dataexchange.meta.util;

import com.sdy.dataexchange.meta.model.MetadataColumn;
import com.sdy.dataexchange.meta.model.MetadataErrorRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.dataexchange.meta.util
 * @date:2019/11/5
 */

@Slf4j
@Component
public class ColumnUtil {
    /**
     * 生成带Key 的 map
     *
     * @param list
     * @return
     */
    private Map<String, MetadataColumn> createMap(List<MetadataColumn> list) {
        Map<String, MetadataColumn> map = new HashMap<String, MetadataColumn>();
        for (MetadataColumn m : list) {
            String key = "";
            String tableName = m.getTableName();
            String columnName = m.getColumnName();
            if (tableName != null && !"".equals(tableName)) {
                key += tableName;
                key += "_";
            }
            if (columnName != null && !"".equals(columnName)) {
                key += columnName;
            }
            map.put(key, m);
        }
        return map;
    }

    /**
     * 对比新数据 与 旧数据 之间的不同
     *
     * @param target
     * @param comMap
     * @return
     */
    private List<MetadataErrorRecord> getDifferentList(Map<String, MetadataColumn> target, Map<String, MetadataColumn> comMap) {
        List<MetadataErrorRecord> merList = new ArrayList<>();
        //判断是否删除表字段
        for (Map.Entry<String, MetadataColumn> entry : target.entrySet()) {
            String key = entry.getKey();
            if (!comMap.containsKey(key)) {
                MetadataErrorRecord m = new MetadataErrorRecord();
                m.setModId(target.get(key).getTableId());
                m.setTableName(target.get(key).getTableName());
                m.setColId(target.get(key).getId());
                m.setColumnName(target.get(key).getColumnName());
                m.setErrorType(11);
                m.setErrorDescribe("元数据表删除了字段 -> " + target.get(key).getColumnName());
                m.setMetadataColumn(target.get(key));
                merList.add(m);
                log.info("字段名:{}  元数据表删除了字段 -> {}", target.get(key).getColumnName(), target.get(key).getColumnName());
            }
        }
        for (Map.Entry<String, MetadataColumn> entry : comMap.entrySet()) {
            String key = entry.getKey();
            if (!target.containsKey(key)) {
                MetadataErrorRecord m = new MetadataErrorRecord();
                m.setModId(comMap.get(key).getTableId());
                m.setTableName(comMap.get(key).getTableName());
                m.setErrorType(12);
                m.setColumnName(comMap.get(key).getColumnName());
                m.setErrorDescribe("元数据表新增了字段 -> " + comMap.get(key).getColumnName());
                m.setMetadataColumn(comMap.get(key));
                merList.add(m);
                log.info("字段名:{}  元数据表添加了字段 -> {}", comMap.get(key).getColumnName(), comMap.get(key).getColumnName());
            } else {
                //新数据
                String columnName = entry.getValue().getColumnName();
                String tableName = entry.getValue().getTableName();
                String dataType = entry.getValue().getDataType();
                Integer dataLength = entry.getValue().getDataLength();
                String dataEmpty = entry.getValue().getDataEmpty();
                String dataKey = entry.getValue().getDataKey();
                MetadataColumn tgt = target.get(key);
                if (!dataType.equals(tgt.getDataType())) {
                    log.info("字段名:{}  字段类型改变:{} -> {} ", columnName, tgt.getDataType(), dataType);
                    MetadataErrorRecord m = new MetadataErrorRecord();
                    m.setTableName(tableName);
                    m.setErrorType(13);
                    m.setColumnName(columnName);
                    m.setModId(tgt.getTableId());
                    m.setColId(tgt.getId());
                    m.setErrorDescribe("字段类型改变: " + tgt.getDataType() + " -> " + dataType);
                    m.setMetadataColumn(comMap.get(key));
                    merList.add(m);
                }
                if (!Objects.equals(tgt.getDataLength(), dataLength)) {
                    log.info("字段名:{}  字段长度改变:{} -> {} ", columnName, tgt.getDataLength(), dataLength);
                    MetadataErrorRecord m = new MetadataErrorRecord();
                    m.setTableName(tableName);
                    m.setErrorType(14);
                    m.setColumnName(columnName);
                    m.setModId(tgt.getTableId());
                    m.setColId(tgt.getId());
                    m.setErrorDescribe("字段长度改变: " + tgt.getDataLength() + " -> " + dataLength);
                    m.setMetadataColumn(comMap.get(key));
                    merList.add(m);
                }
                if (!Objects.equals(tgt.getDataEmpty(), dataEmpty)) {
                    log.info("字段名:{}  字段是否为空改变:{} -> {} ", columnName, tgt.getDataEmpty(), dataEmpty);
                    MetadataErrorRecord m = new MetadataErrorRecord();
//                    m.setDbId(114);
                    m.setTableName(tableName);
                    m.setErrorType(15);
                    m.setColumnName(columnName);
                    m.setModId(tgt.getTableId());
                    m.setColId(tgt.getId());
                    m.setErrorDescribe("字段是否为空改变: " + tgt.getDataEmpty() + " -> " + dataEmpty);
                    m.setMetadataColumn(comMap.get(key));
                    merList.add(m);
                }
                if (!Objects.equals(tgt.getDataKey(), dataKey)) {
                    log.info("字段名:{}  字段是否主键改变:{} -> {} ", columnName, tgt.getDataKey(), dataKey);
                    MetadataErrorRecord m = new MetadataErrorRecord();
//                    m.setDbId(114);
                    m.setTableName(tableName);
                    m.setErrorType(16);
                    m.setColumnName(columnName);
                    m.setModId(tgt.getTableId());
                    m.setColId(tgt.getId());
                    m.setErrorDescribe("字段是否主键改变: " + tgt.getDataKey() + " -> " + dataKey);
                    m.setMetadataColumn(comMap.get(key));
                    merList.add(m);
                }

            }
        }
        return merList;
    }

    public List<MetadataErrorRecord> getComResulMap(List<MetadataColumn> formerData, List<MetadataColumn> newData) {
        Map<String, MetadataColumn> formerDataMap = createMap(formerData);
        Map<String, MetadataColumn> newDataMap = createMap(newData);
        List<MetadataErrorRecord> reduce = getDifferentList(formerDataMap, newDataMap);
        return reduce;
    }
//    public static void main(String[] args) {
//
//        ListUtils listUtils = new ListUtils();
//        List<MetadataColumn> formerData = new ArrayList<>();
//        MetadataColumn metadataColumn = new MetadataColumn();
//
//        metadataColumn.setColumnName("wyy");
//        metadataColumn.setDataType("CHAR");
//        metadataColumn.setDataLength(11);
//        metadataColumn.setTableName("tset1");
//        formerData.add(metadataColumn);
//
//        MetadataColumn metadataColumn4 = new MetadataColumn();
//
//        metadataColumn4.setColumnName("wyy3");
//        metadataColumn4.setDataType("VARCHAR2");
//        metadataColumn4.setTableName("tset1");
//        formerData.add(metadataColumn4);
//
//        List<MetadataColumn> newData = new ArrayList<>();
//        MetadataColumn metadataColumn2 = new MetadataColumn();
//
//        metadataColumn2.setColumnName("wyy");
//        metadataColumn2.setDataType("VARCHAR");
//        metadataColumn2.setDataLength(12);
//        metadataColumn2.setTableName("tset1");
//        newData.add(metadataColumn2);
//
//        MetadataColumn metadataColumn3 = new MetadataColumn();
//        metadataColumn3.setColumnName("wyy2");
//        metadataColumn3.setDataType("VARCHAR");
//        metadataColumn3.setTableName("tset1");
//        newData.add(metadataColumn3);
//
////        Map<String, List<MetadataColumn>> map = listUtils.getComResulMap(formerData,newData);
//
//        List<MetadataErrorRecord> map = listUtils.getComResulMap(newData,formerData);
//        System.out.println(map.toString());
//    }
}
